#include "image2binwidget.h"
#include <QHeaderView>
#include <QFileDialog>
#include <QDebug>
#include <QStandardItem>
#include <QMessageBox>

Image2binWidget::Image2binWidget(QWidget *parent)
    : QWidget(parent)
{
    fileNames.clear();
    loadFileButton = new QPushButton("加载");
    transButton = new QPushButton("转换");

    imageLable = new QLabel("请选择图片");
    imageLable->setAlignment(Qt::AlignCenter);

    outImageWidthLabel = new QLabel("宽");
    outImageWidthSpinBox = new QSpinBox;
    outImageWidthSpinBox->setRange(1, 2000);
    outImageWidthSpinBox->setValue(100);

    outImageHeightLabel = new QLabel("高");
    outImageHeightSpinBox = new QSpinBox;
    outImageHeightSpinBox->setRange(1, 2000);
    outImageHeightSpinBox->setValue(100);





    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(imageLable, 0, 0, 6, 22);
    mainLayout->addWidget(outImageWidthLabel, 6, 0, 1, 1);
    mainLayout->addWidget(outImageWidthSpinBox, 6, 1, 1, 10);
    mainLayout->addWidget(outImageHeightLabel, 6, 11, 1, 1);
    mainLayout->addWidget(outImageHeightSpinBox, 6, 12, 1, 10);

    mainLayout->addWidget(loadFileButton, 7, 0, 1, 11);
    mainLayout->addWidget(transButton, 7, 11, 1, 11);



    setLayout(mainLayout);

    resize(250, 200);


    connect(loadFileButton, &QPushButton::clicked, this, &Image2binWidget::LoadImageSlot);
    connect(transButton, &QPushButton::clicked, this, &Image2binWidget::TransImageSlot);
}

Image2binWidget::~Image2binWidget()
{
}


void Image2binWidget::LoadImageSlot()
{
    fileNames = QFileDialog::getOpenFileNames(this);
}
void Image2binWidget::TransImageSlot()
{
    QByteArray imageDatArr;
    QImage  *image;
    imageDatArr.clear();

    QStringList::const_iterator it;
    QString indexInfo;
    for (it = fileNames.constBegin(); it != fileNames.constEnd(); it++)
    {
        image = new QImage(*it);
        if(image == nullptr)
        {
            QMessageBox::information(this, "提示", "图片格式不支持");
        }
        imageLable->setPixmap(QPixmap::fromImage(image->scaled(imageLable->width(), imageLable->height())));

        *image = image->scaled(outImageWidthSpinBox->value(), outImageHeightSpinBox->value());
        *image = image->convertToFormat(QImage::Format_RGB16);

        const uchar *p = image->constBits();

        indexInfo.append(QString("0x%1").arg(imageDatArr.size(), 8, 16, QChar('0')) + "-->" + *it);
        indexInfo.append(QString("(%1*%2)").arg(image->width()).arg(image->height()) + "\r\n");

        imageDatArr.append((const char*)p, image->sizeInBytes());
        delete image;
        image = nullptr;
    }

    QString outLayoutFilename("./out_layout.txt");
    if(QFile::exists(outLayoutFilename))
    {
        QFile::remove(outLayoutFilename);
    }

    QFile layoutFile(outLayoutFilename);
    if(layoutFile.open(QIODevice::WriteOnly))
    {
        layoutFile.write(indexInfo.toUtf8());
        layoutFile.close();
    }
    else
    {
        QMessageBox::information(this, "提示", "打开输出信息文件失败");
    }

    QString outFilename("./out.bin");
    if(QFile::exists(outFilename))
    {
        QFile::remove(outFilename);
    }
    QFile outFile(outFilename);
    if(outFile.open(QIODevice::WriteOnly))
    {
        outFile.write(imageDatArr);
        outFile.close();
    }
    else
    {
        QMessageBox::information(this, "提示", "打开输出bin文件失败");
    }


}
